package com.hua.controller;

import com.hua.service.GetDataService;
import com.hua.utils.Read_utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class GetDataController {

    private static Map<String,String> natality_map;
    private static Map<String,String> population_map;
    private static Map<String,String> lifetime_map;
    @Resource
    GetDataService getDataService;

    static {
        natality_map = new HashMap<>();
        natality_map.put("人口自然增长率","A030203");
        natality_map.put("人口死亡率","A030202");
        natality_map.put("人口出生率","A030201");
        population_map = new HashMap<>();
        population_map.put("年末常住人口","A030101");
        population_map.put("城镇人口","A030102");
        population_map.put("乡村人口","A030103");
        lifetime_map = new HashMap<>();

    }

    @GetMapping
    public Map<String,Map> get_data() throws IOException {
        Map<String,Map> result = new HashMap<>();
        List<String> natality_files = Read_utils.read_file("natality");
        List<String> population_files = Read_utils.read_file("population");
        List<String> list = Read_utils.read_lifetime();
        for (int i = 0; i < natality_files.size(); i++) {
            int finalI = i;
            //获取省名
            String cityName = getDataService.get_city_name(natality_files.get(finalI));
            Map<String,Map> aa = new HashMap<>();
            natality_map.forEach((detail, code) -> {
                Map<String, String> nasality = getDataService.get_natality(natality_files.get(finalI),code);
                aa.put(detail,nasality);
            });
            population_map.forEach((detail,code) -> {
                Map<String, String> population = getDataService.get_natality(population_files.get(finalI),code);
                aa.put(detail,population);
            });
            result.put(cityName,aa);

        }
        for (String s : list) {
            String[] split = s.split(",");
            Map map = result.get(split[0]);
            Map map1 = new HashMap();
            map1.put("2020",split[1]);
            map.put("平均预期寿命(岁)",map1);
            Map map2 = new HashMap();
            map2.put("2020",split[2]);
            map.put("男性平均预期寿命(岁)",map2);
            Map map3 = new HashMap();
            map3.put("2020",split[3]);
            map.put("女性平均预期寿命(岁)",map3);
        }
        return result;
    }

}
